/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package index.inverted;

import compression.Kompresi;
import index.Reader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Danang
 */
public class InvertedFileReader extends Reader {
    public final static String INVERTED_FILE = "invertedindexfile"; // final build for inverted index file
    
    boolean useCompress;
    
    public InvertedFileReader(String rootDir, boolean useCompress) throws FileNotFoundException {
        super(rootDir+INVERTED_FILE);
        this.useCompress = useCompress;
    }
    
    public synchronized String readPostingList(long pos, int bytes) throws IOException {
        if (!useCompress) {
            return this.readBytes(pos, bytes);
        }
        String str = this.readBytes(pos, bytes);
        ArrayList<ArrayList<Integer>> res = Kompresi.DecodeGabung(str);
        StringBuilder sb = new StringBuilder();
        for (ArrayList<Integer> l : res) {
            for (Integer i : l) {
                sb.append(i).append(",");
            }
            //sb.deleteCharAt(sb.length()-1);
            sb.append("\n");
        }
        return sb.toString();
    }
    
}
